<template>
  <div class="dashboard-container">
    <el-container class="main-container">
      <el-header height="60px">
        <div class="header-left">
          <h1>在线考试系统 - 学生平台</h1>
        </div>
        <div class="header-right">
          <el-dropdown>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="handleLogout">退出登录</el-dropdown-item>
                <el-dropdown-item @click="handleChangePassword">修改密码</el-dropdown-item>
              </el-dropdown-menu>
            </template>
            <el-button type="primary">
              {{ userInfo.username }} - {{ userInfo.roleName }}
              <el-icon>
                <ArrowDown />
              </el-icon>
            </el-button>
          </el-dropdown>
        </div>
      </el-header>
      <el-container>
        <el-aside width="200px" class="sidebar">
          <el-menu :default-openeds="['1', '2', '3']" router>
            <el-menu-item index="/student/my-exams">
            <el-icon><Calendar /></el-icon>
            <span>我的考试</span>
          </el-menu-item>
            <el-sub-menu index="2">
              <template #title>
                <el-icon>
                  <DataAnalysis />
                </el-icon>
                <span>成绩查询</span>
              </template>
              <el-menu-item index="/student/score/list">成绩列表</el-menu-item>
              <el-menu-item index="/student/score/detail">成绩详情</el-menu-item>
            </el-sub-menu>
            <el-sub-menu index="3">
              <template #title>
                <el-icon>
                  <Document />
                </el-icon>
                <span>课程管理</span>
              </template>
              <el-menu-item index="/student/course/selected">已选课程</el-menu-item>
              <el-menu-item index="/student/course/selection">课程选择</el-menu-item>
            </el-sub-menu>
          </el-menu>
        </el-aside>
        <el-main>
          <div class="content">
            <el-card>
              <template #header>
                <div class="card-header">
                  <span>学生中心</span>
                </div>
              </template>
              <div class="dashboard-stats">
                <el-row :gutter="20">
                  <el-col :span="6">
                    <el-card class="stat-card">
                      <div class="stat-content">
                        <div class="stat-number">3</div>
                        <div class="stat-label">当前考试</div>
                      </div>
                    </el-card>
                  </el-col>
                  <el-col :span="6">
                    <el-card class="stat-card">
                      <div class="stat-content">
                        <div class="stat-number">8</div>
                        <div class="stat-label">已选课程</div>
                      </div>
                    </el-card>
                  </el-col>
                  <el-col :span="6">
                    <el-card class="stat-card">
                      <div class="stat-content">
                        <div class="stat-number">85.5</div>
                        <div class="stat-label">平均成绩</div>
                      </div>
                    </el-card>
                  </el-col>
                  <el-col :span="6">
                    <el-card class="stat-card">
                      <div class="stat-content">
                        <div class="stat-number">12</div>
                        <div class="stat-label">历史考试</div>
                      </div>
                    </el-card>
                  </el-col>
                </el-row>
              </div>

              <div class="dashboard-info">
                <h3>欢迎使用学生平台！</h3>
                <p>作为学生，您可以在此查看考试安排、参加在线考试、查询成绩和管理课程。</p>
                <p>请使用左侧菜单进行相应操作。</p>
                <el-divider />
                <h4>我的考试安排</h4>
                <el-table :data="upcomingExams" style="width: 100%">
                  <el-table-column prop="examName" label="考试名称"></el-table-column>
                  <el-table-column prop="courseName" label="课程"></el-table-column>
                  <el-table-column prop="startTime" label="开始时间"></el-table-column>
                  <el-table-column prop="duration" label="时长"></el-table-column>
                </el-table>
              </div>
            </el-card>
          </div>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Calendar, DataAnalysis, Document, ArrowDown } from '@element-plus/icons-vue'
import { get } from '../../utils/api'

const router = useRouter()
const userInfo = ref({ username: '', roleId: '', roleName: '' })

const upcomingExams = ref([
  { examName: '高等数学期末考试', courseName: '高等数学', startTime: '2024-01-20 09:00', duration: '120分钟' },
  { examName: '线性代数期中考试', courseName: '线性代数', startTime: '2024-01-22 14:00', duration: '90分钟' },
  { examName: '概率论与数理统计测验', courseName: '概率论', startTime: '2024-01-25 10:00', duration: '60分钟' }
])

onMounted(() => {
  // 从本地存储获取用户信息
  const storedUserInfo = localStorage.getItem('userInfo')
  if (storedUserInfo) {
    userInfo.value = JSON.parse(storedUserInfo)
  } else {
    // 如果没有用户信息，跳转到登录页面
    router.push('/login')
  }
})

const handleLogout = async () => {
  try {
    // 调用后端登出接口，带上cookie
    await get('/user/logout')
    
    // 清除本地存储的用户信息
    localStorage.removeItem('user')
    
    ElMessage.success('退出登录成功')
    router.push('/login')
  } catch (error) {
    console.error('退出登录失败:', error)
    // 即使后端调用失败，也要清除本地状态并跳转
    localStorage.removeItem('user')
    ElMessage.success('退出登录成功')
    router.push('/login')
  }
}

const handleChangePassword = () => {
  router.push('/student/change-password')
}
</script>

<style scoped>
.dashboard-container {
  width: 100vw;
  height: 100vh;
  padding: 0;
  margin: 0;
  box-sizing: border-box;
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  z-index: 1;
  overflow: hidden;
}

.main-container {
  height: 100%;
  width: 100%;
  display: flex;
  flex-direction: column;
}

.el-header {
  background-color: #52c41a;
  color: #fff;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
}

.header-left h1 {
  margin: 0;
  font-size: 20px;
}

.sidebar {
  background-color: #f0f9f0;
  overflow-y: auto;
}

.el-menu {
  background-color: transparent;
  border-right: none;
}

.el-menu-item {
  color: #000;
}

.el-menu-item:hover {
  background-color: #52c41a;
  color: #000;
}

.el-menu-item.is-active {
  background-color: #52c41a;
  color: #000;
}

.el-sub-menu__title {
  color: #000;
}

.el-sub-menu__title:hover {
  background-color: #52c41a;
  color: #000;
}

.content {
  padding: 20px;
  background-color: #f5f7fa;
  height: 100%;
  overflow-y: auto;
}

.dashboard-stats {
  margin-bottom: 20px;
}

.stat-card {
  height: 120px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
  color: white;
}

.stat-number {
  font-size: 36px;
  font-weight: bold;
  margin-bottom: 10px;
}

.stat-label {
  font-size: 16px;
}

.dashboard-info {
  padding: 20px;
}

.dashboard-info h3,
.dashboard-info h4 {
  color: #333;
  margin-bottom: 10px;
}

.dashboard-info p {
  color: #666;
  line-height: 1.8;
  margin-bottom: 15px;
}
</style>